package com.liu.frame.http;

import android.content.Context;

import com.liu.frame.application.MyApplication;
import com.liu.frame.utils.GsonUtils;
import com.liu.frame.utils.LoadingUtil;
import com.liu.frame.utils.LogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.List;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import okhttp3.Headers;

/**
 * Created by 刘智强 on 2016/10/21.
 */

public abstract class WeiLiCaiHttpRequestCallBack<T> extends BaseHttpRequestCallback {
    private Context context; //上下文
    private String code; //网络请求获得的状态码
    private String message;//网络请求获得的服务器返回的信息
    private String data;
    private Class<T> t;//传入需要GSON解析成的Bean的类型

    public WeiLiCaiHttpRequestCallBack(Context context, Class<T> t) {
        this.context = context;
        this.t = t;
    }

    /**
     * 开始请求调用
     */
    @Override
    public void onStart() {
        super.onStart();
        LogUtils.e(MyApplication.getInstance().appVersion);
    }


    /**
     * 请求完成
     */
    @Override
    public void onFinish() {
        super.onFinish();
        LoadingUtil.getInstance().dismissDialog();

    }


    /**
     * 请求成功
     *
     * @param response
     */
    @Override
    public void onResponse(String response, Headers headers) {
        super.onResponse(response, headers);
        data = GsonUtils.json2String(response, "data");
        code = GsonUtils.json2String(response, "returnCode");
        String message = GsonUtils.json2String(response, "returnMessage");
        if (code.isEmpty()) {
            code = "0";
        }
        if (message.isEmpty()) {
            message = "";
        }
        List<T> list = null;

        if (data.isEmpty()) {
            LogUtils.e("服务器没有返回数据");
            return;
        }
        try {
            Object json = new JSONTokener(data).nextValue();
            /**
             * 属于json对象
             */
            if (json instanceof JSONObject) {
                success(code, message, GsonUtils.fromJson(data, t));
            } else if (json instanceof JSONArray) {
                /**
                 * 属于json数组
                 */
                list = GsonUtils.fromJsonArray(data, t);
                success(code, message, list);
                LogUtils.e(list.toString());
            }

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e("异常信息->" + e.toString());
        }
        if (code.equals("00000")) {
            success(code, message, list);
        }

    }

    /**
     * 请求失败
     *
     * @param errorCode
     * @param msg
     */
    @Override
    public void onFailure(int errorCode, String msg) {
        super.onFailure(errorCode, msg);
        fail(String.valueOf(errorCode), msg);
    }

    /**
     * 网络请求成功
     *
     * @param code
     * @param message
     * @param datas
     */
    public abstract void success(String code, String message, List<T> datas);//服务器返回json数组调用

    public abstract void success(String coe, String message, T data);//服务器返回的json对象调用

    public abstract void fail(String code, String message);
}
